package com.endi.sap.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration //标识配置类
@EnableWebSecurity //开启springsecurity的自定义配置（springboot项目中该注解可以省略，有预定义配置）
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

//    @Bean
//    public PasswordEncoder BrPasswordEncoders(){
//        return new BCryptPasswordEncoder();
//    }

    private static final String username = "APP_M4IQ6CUEU5G6MM0C1V35";

    private static final String password = "PEC66NA176DK5DWQ7XP89ATM2CNZ1PS9B25VLPDD";
    @Override // 配置用户认证, 使用自定义身份验证组件
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser(username).password("{noop}"+ password).roles("ADMIN");
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/public/**").permitAll() // 开放接口，不需要权限
                .anyRequest().authenticated() // 其他接口需要认证
                .and()
                .httpBasic()
                .and()
                .csrf()
                .disable();
    }


}